﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class CompanyManage : BasePage
    {
        private Sys_CompanyBLL scb = new Sys_CompanyBLL();
        private Sys_DepartmentBLL sdb = new Sys_DepartmentBLL();
        private Sys_UserBLL sub = new Sys_UserBLL();
        private static DataTable dtCompany = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                div_Dept.Visible = false;
                BindCom();
                BindCompany();
                BindCompanyPerson();
            }
        }

        private void setList(DataTable dt, System.Web.UI.WebControls.DropDownList lb)
        {
            int idx = -1;
            dtCompany = dt;
            for (int n = 0; n < dtCompany.Rows.Count; n++)
            {
                if (dtCompany.Rows[n]["cmy_parentid"].ToString() == "0")
                {
                    idx = idx + 1;
                    addItem(lb, dtCompany.Rows[n]["CMY_ID"].ToString(), dtCompany.Rows[n]["CMY_NAME"].ToString(), idx);
                    idx = idx - 1;
                }
            }
        }

        private void addItem(System.Web.UI.WebControls.DropDownList lb, String sId, String sName, int idx)
        {
            String sn = sName;
            for (int n = 0; n < idx; n++)
            {
                sn = (char)58853 + "" + sn;
            }
            System.Web.UI.WebControls.ListItem it = new ListItem(sn, sId);

            lb.Items.Add(it);

            for (int n = 0; n < dtCompany.Rows.Count; n++)
            {
                if (dtCompany.Rows[n]["cmy_parentid"].ToString() == sId)
                {
                    idx = idx + 1;
                    addItem(lb, dtCompany.Rows[n]["cmy_id"].ToString(), dtCompany.Rows[n]["cmy_name"].ToString(), idx);
                    idx = idx - 1;
                }
            }
        }

        private void BindCompany()
        {
            //绑定公司下拉框
            //string cpId = scb.GetInfoById(lognUser.USER_COMPANY.ToString()).CMY_PARENTID.ToString() == "0" ? scb.GetInfoById(lognUser.USER_COMPANY.ToString()).CMY_ID.ToString() : scb.GetInfoById(lognUser.USER_COMPANY.ToString()).CMY_PARENTID.ToString();

            DataTable dt = scb.GetInfoByPId(1);//父级所有公司
            ddlParentCompany.DataSource = dt;
            ddlParentCompany.DataTextField = "CMY_NAME";
            ddlParentCompany.DataValueField = "CMY_ID";
            ddlParentCompany.DataBind();
            //setList(dt, ddlParentCompany);

            ddlParentCompany.Items.Insert(0, new ListItem("---请选择---", "-1"));
            ddlParentCompany.SelectedValue = "-1";

            //setList(dt, ddlDeptCompany);
            ddlDeptCompany.Items.Insert(0, new ListItem("---请选择---", "-1"));
            ddlDeptCompany.SelectedValue = "-1";

            //绑定部门下拉框GetInfoByCmyID

            //DataTable dtdept = sdb.GetInfoALLCmyID("-1");
            //ddlUpDept.DataSource = dtdept;
            //ddlUpDept.DataTextField = "DEPTNAME";
            //ddlUpDept.DataValueField = "DEPTID";
            //ddlUpDept.DataBind();
            ddlUpDept.Items.Insert(0, new ListItem("---请选择---", "-1"));
            ddlUpDept.SelectedValue = "-1";
        }

        private void BindCompanyPerson()
        {
            /*
            string deptid = "";
            DataTable cmyPersonId = scb.GetUserCompanyPerson(deptid);
            ddlCompanyPerson.DataSource = cmyPersonId;
            ddlCompanyPerson.DataTextField = "user_name";
            ddlCompanyPerson.DataValueField = "user_id";
            ddlCompanyPerson.DataBind();
            ddlCompanyPerson.Items.Insert(0, new ListItem("---请选择---", "-1"));
            ddlCompanyPerson.SelectedValue = "-1";
            */
        }

        private void BindCom()
        {
            string CID = string.IsNullOrEmpty(hidCompanyID.Value) ? lognUser.USER_COMPANY.ToString() : hidCompanyID.Value;
            DataTable dt = scb.GetInfoByPId(Convert.ToInt32(CID));

            gvCompany.DataSource = dt;
            gvCompany.DataBind();
        }

        private void BindDept()
        {
            NewMethod();
            string CID = string.IsNullOrEmpty(hidCompanyID.Value) ? "1" : hidCompanyID.Value;
            DataTable dt = null;
            if (!string.IsNullOrEmpty(hidCompanyID.Value))
            {
                if (hidKey.Value == "0")
                {
                    dt = sdb.GetInfoByCmyID(CID, true);
                }
                else
                {
                    dt = sdb.GetInfoByCmyID(hidCompanyID.Value, false);
                    //dt = sdb.GetInfoCmyID(hidCompanyID.Value);
                }
            }
            else
            {
                dt = sdb.GetInfoByCmyID(lognUser.USER_COMPANY.ToString(), true);
            }

            gvDept.DataSource = dt;
            gvDept.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(hidKey.Value))
            {
                if (hidKey.Value == "0")
                {
                    div_Company.Visible = true;
                    div_Dept.Visible = false;
                    BindCom();
                }
                else
                {
                    div_Company.Visible = false;
                    div_Dept.Visible = true;
                    BindDept();
                }
            }
        }

        /// <summary>
        /// 组织机构新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCompanyName.Text.Trim()))
            {
                lblMessage.Text = "公司名称不能为空";
                return;
            }
            /*
            if (ddlCompanyPerson.SelectedValue.ToString() == "-1")
            {
                lblMessage.Text = "公司领导不能为空";
                return;
            }*/
            Sys_Company model = new Sys_Company();

            model.CMY_NAME = txtCompanyName.Text;
            model.CMY_NAME2 = txtAlias.Text;
            model.CMY_LEVEL = decimal.Parse(txtLevel.Text == "" ? "1" : txtLevel.Text);
            model.CMY_TYPE = ddlCompanyType.SelectedValue;
            model.CMY_PARENTID = decimal.Parse(ddlParentCompany.SelectedValue == "-1" ? "0" : ddlParentCompany.SelectedValue);
            model.CMY_PAESONID = 0;
            model.C_CODE = TextBox1.Text.Trim().ToUpper();

            for (int i = 0; i < gvCompany.Rows.Count; i++)
            {
                string lblcmName = gvCompany.Rows[i].Cells[1].Text;
                if (lblcmName == txtCompanyName.Text)
                {
                    lblMessage.Text = "该公司已经存在";
                    return;
                }
            }

            int count = scb.INSERT(model, lognUser.USER_NAME);
            if (count > 0)
            {
                lblMessage.Text = "保存成功";
                BindCom();
            }
            else
            {
                lblMessage.Text = "保存失败";
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCompanyName.Text.Trim()))
            {
                lblMessage.Text = "公司名称不能为空";
                return;
            }

            Sys_Company model = new Sys_Company();
            model.CMY_ID = decimal.Parse(lblID.Text);
            model.CMY_NAME = txtCompanyName.Text;
            model.CMY_NAME2 = txtAlias.Text;
            model.CMY_LEVEL = decimal.Parse(txtLevel.Text);
            model.CMY_TYPE = ddlCompanyType.SelectedValue;
            model.CMY_PARENTID = decimal.Parse(ddlParentCompany.SelectedValue == "-1" ? "0" : ddlParentCompany.SelectedValue);
            //model.CMY_PAESONID = decimal.Parse(ddlCompanyPerson.SelectedValue == "-1" ? "0" : ddlCompanyPerson.SelectedValue);

            model.C_CODE = TextBox1.Text.Trim().ToUpper();
            int count = scb.UPDATE(model, lognUser.USER_NAME);
            if (count > 0)
            {
                lblMessage.Text = "保存成功";
                BindCom();
            }
            else
            {
                lblMessage.Text = "保存失败";
            }
        }

        protected void btnDelele_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(lblID.Text))
            {
                lblMessage.Text = "请选择要删除的公司";
            }
            else
            {
                int count = scb.DELETE(lblID.Text, lognUser.USER_NAME);
                if (count > 0)
                {
                    lblMessage.Text = "删除成功";
                    BindCom();
                }
                else
                {
                    lblMessage.Text = "删除失败";
                }
            }
        }

        protected void ddlDeptCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            NewMethod();
        }

        private void NewMethod()
        {
            //绑定部门下拉框GetInfoByCmyID
            DataTable dtdept = sdb.GetInfoALLCmyID(ddlDeptCompany.SelectedValue);
            ddlUpDept.DataSource = dtdept;
            ddlUpDept.DataTextField = "DEPTNAME";
            ddlUpDept.DataValueField = "DEPTID";
            ddlUpDept.DataBind();
            ddlUpDept.Items.Insert(0, new ListItem("---请选择---", "-1"));
            ddlUpDept.SelectedValue = "-1";
        }

        protected void gvCompany_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            //string ids = ((Label)gvCompany.Rows[e.NewSelectedIndex].FindControl("lblcmId")).Text;
            string ids = gvCompany.Rows[e.NewSelectedIndex].Cells[0].Text;
            Sys_Company model = scb.GetInfoById(ids);

            lblID.Text = model.CMY_ID.ToString();
            txtCompanyName.Text = model.CMY_NAME;
            txtAlias.Text = model.CMY_NAME2;
            txtLevel.Text = model.CMY_LEVEL.ToString();

            TextBox1.Text = model.C_CODE;
            //ddlCompanyType.SelectedValue = model.CMY_TYPE.ToString() == "分公司" ? "0" : model.CMY_TYPE == "控股公司" ? "1" : model.CMY_TYPE == "总公司" ? "-1" : "";
            ddlCompanyType.SelectedValue = model.CMY_TYPE.ToString().Trim();
            ddlParentCompany.SelectedValue = model.CMY_PARENTID.ToString() == "0" ? "-1" : model.CMY_PARENTID.ToString();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            foreach (GridViewRow count in gvCompany.Rows)
            {
                if (count.RowType == DataControlRowType.DataRow)
                {
                    count.Attributes["onclick"] = ClientScript.GetPostBackEventReference(gvCompany, "Select$" + count.RowIndex.ToString(), true);
                    count.Attributes["style"] = "cursor:pointer";
                    count.Attributes["title"] = "单击选择行";
                }
            }
            foreach (GridViewRow count in gvDept.Rows)
            {
                if (count.RowType == DataControlRowType.DataRow)
                {
                    count.Attributes["onclick"] = ClientScript.GetPostBackEventReference(gvDept, "Select$" + count.RowIndex.ToString(), true);
                    count.Attributes["style"] = "cursor:pointer";
                    count.Attributes["title"] = "单击选择行";
                }
            }
            base.Render(writer);
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string sql = "";
            //公司名称
            string comName = txtCompanyName.Text.Trim();
            if (!string.IsNullOrEmpty(comName))
            {
                sql += " AND  CMY_NAME like '%" + comName + "%'";
            }
            //公司别名
            string comName2 = txtAlias.Text.Trim();
            if (!string.IsNullOrEmpty(comName2))
            {
                sql += "  AND CMY_NAME2 like '%" + comName2 + "%'";
            }
            //公司层级
            string comLeval = txtLevel.Text.Trim();
            if (!string.IsNullOrEmpty(comLeval))
            {
                sql += "  AND CMY_LEVEL=" + comLeval + " ";
            }
            //公司类型
            string comType = ddlCompanyType.SelectedValue.ToString();
            if (!string.IsNullOrEmpty(comType))
            {
                sql += "   AND CMY_TYPE=" + comType + " ";
            }
            //上级公司
            string ComfathId = ddlParentCompany.SelectedValue.ToString();
            if (ComfathId != "-1")
            {
                sql += "  AND CMY_PARENTID=" + ComfathId + " ";
            }
            string txtDaima = TextBox1.Text.ToString();
            if (txtDaima != "")
            {
                sql += "  AND C_CODE like '%" + txtDaima + "%'";
            }
            DataTable dt = scb.SelectByWhere(sql, lognUser.USER_COMPANY.ToString());
            gvCompany.DataSource = dt;
            gvCompany.DataBind();
        }

        protected void btnSelectDept_Click(object sender, EventArgs e)
        {
            string sql = "";
            //部门名称
            if (!string.IsNullOrEmpty(txtDeptName.Text.Trim()))
            {
                sql += @" AND sysd.deptname like '%" + txtDeptName.Text.Trim() + "%'";
            }
            //所属公司
            if (ddlDeptCompany.SelectedValue.ToString() != "-1")
            {
                sql += @" AND sysd.cmy_id = " + ddlDeptCompany.SelectedValue.ToString() + " ";
            }
            //排序号
            if (!string.IsNullOrEmpty(txtDeptNumber.Text.Trim()))
            {
                sql += @" AND sysd.deptnumber =" + txtDeptNumber.Text.Trim() + " ";
            }
            //上级部门
            if (ddlUpDept.SelectedValue.ToString() != "-1")
            {
                sql += @" AND sysd.deptparentid = " + ddlUpDept.SelectedValue.ToString() + " ";
            }
            //部门领导
            if (!string.IsNullOrEmpty(HId1.Value))
            {
                sql += @" AND sysd.dept_personid = " + HId1.Value + " ";
            }
            //分管领导
            if (!string.IsNullOrEmpty(HId2.Value))
            {
                sql += @" AND sysd.dept_depersonid = " + HId2.Value + " ";
            }
            //绩效考核负责人
            if (!string.IsNullOrEmpty(HId3.Value))
            {
                sql += @" AND sysd.dept_jxpersonid = " + HId3.Value + " ";
            }
            DataTable dt = sdb.DeptSelect(sql, lognUser.USER_DEPARTMENT.ToString());
            gvDept.DataSource = dt;
            gvDept.DataBind();
        }

        /// <summary>
        /// 多余新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsertDept_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDeptName.Text.Trim()))
            {
                lblMes.Text = "部门名称不能为空";
                //MessageBox.Show(this, "部门名称不能为空");
                return;
            }
            if (ddlDeptCompany.SelectedValue == null || ddlDeptCompany.SelectedValue == "-1")
            {
                lblMes.Text = "请选择所属公司";
                //MessageBox.Show(this, "请选择所属公司");
                return;
            }
            if (DropDownList1.SelectedValue == null || DropDownList1.SelectedValue == "-1")
            {
                lblMes.Text = "请选择部门级别";
                //MessageBox.Show(this, "请选择部门级别");
                return;
            }
            if (DropDownList2.SelectedValue == null || DropDownList2.SelectedValue == "-1")
            {
                lblMes.Text = "请选择有效状态";
                //MessageBox.Show(this, "请选择有效状态");
                return;
            }
            if (txtDeptNumber.Text == "")
            {
                lblMes.Text = "排序号不能为空";
                //MessageBox.Show(this, "排序号不能为空");
                return;
            }
            Sys_Department model = new Sys_Department();

            model.CMY_ID = decimal.Parse(ddlDeptCompany.SelectedValue);
            model.DEPTNAME = txtDeptName.Text;
            model.DEPTNUMBER = decimal.Parse(txtDeptNumber.Text == "" ? "0" : txtDeptNumber.Text);
            model.DEPTPARENTID = decimal.Parse(ddlUpDept.SelectedValue == "-1" ? "0" : ddlUpDept.SelectedValue);
            model.DEPT_PERSONID = decimal.Parse(HId1.Value == "" ? "0" : HId1.Value);
            model.DEPT_DEPERSONID = decimal.Parse(HId2.Value == "" ? "0" : HId2.Value);
            model.DEPT_JXPERSONID = decimal.Parse(HId3.Value == "" ? "0" : HId3.Value);

            model.DLEVEL = int.Parse(DropDownList1.SelectedValue.ToString());
            model.STATE = int.Parse(DropDownList2.SelectedValue.ToString());

            int count = sdb.INSERT(model, lognUser.USER_NAME);
            if (count > 0)
            {
                lblMes.Text = "保存成功";
                BindDept();
            }
            else
            {
                lblMes.Text = "保存失败";
            }
        }

        protected void btnUpdateDept_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDeptName.Text.Trim()))
            {
                lblMes.Text = "部门名称不能为空";
                //MessageBox.Show(this, "部门名称不能为空");
                return;
            }
            if (ddlDeptCompany.SelectedValue == null || ddlDeptCompany.SelectedValue == "")
            {
                lblMes.Text = "所属公司不能为空";
                //MessageBox.Show(this, "请选择所属公司");
                return;
            }
            if (DropDownList1.SelectedValue == null || DropDownList1.SelectedValue == "")
            {
                lblMes.Text = "请选择部门级别";
                //MessageBox.Show(this, "请选择部门级别");
                return;
            }
            if (DropDownList2.SelectedValue == null || DropDownList2.SelectedValue == "")
            {
                lblMes.Text = "请选择有效状态";
                //MessageBox.Show(this, "请选择有效状态");
                return;
            }
            if (txtDeptNumber.Text == "")
            {
                lblMes.Text = "排序号不能为空";
                //MessageBox.Show(this, "排序号不能为空");
                return;
            }
            //if (ddlDeptPerson.SelectedValue.ToString() == "-1")
            //{
            //    lblMes.Text = "部门领导不能为空";
            //    return;
            //}
            //if (ddlDeptDPerson.SelectedValue.ToString() == "-1")
            //{
            //    lblMes.Text = "分管领导不能为空";
            //    return;
            //}
            //if (ddlDeptJXPerson.SelectedValue.ToString() == "-1")
            //{
            //    lblMessage.Text = "绩效考核负责人不能为空";
            //    return;
            //}

            Sys_Department model = new Sys_Department();

            model.CMY_ID = decimal.Parse(ddlDeptCompany.SelectedValue);
            //if (lblDeptID.Text == "" || lblDeptID.Text == null)
            //{
            //    MessageBox.Show(Page, "请选择本公司内部人员！");
            //    return;
            //}
            //else
            //{
            model.DEPTID = decimal.Parse(lblDeptID.Text);
            //}
            model.DEPTNAME = txtDeptName.Text;
            model.DEPTNUMBER = decimal.Parse(txtDeptNumber.Text);
            model.DEPTPARENTID = decimal.Parse(ddlUpDept.SelectedValue == "" ? "0" : ddlUpDept.SelectedValue);

            model.DEPT_PERSONID = decimal.Parse(HId1.Value == "" ? "0" : HId1.Value);
            model.DEPT_DEPERSONID = decimal.Parse(HId2.Value == "" ? "0" : HId2.Value);
            model.DEPT_JXPERSONID = decimal.Parse(HId3.Value == "" ? "0" : HId3.Value);

            model.DLEVEL = int.Parse(DropDownList1.SelectedValue.ToString());
            model.STATE = int.Parse(DropDownList2.SelectedValue.ToString());

            int count = sdb.UPDATE(model, lognUser.USER_NAME);
            if (count > 0)
            {
                lblMes.Text = "保存成功";
                BindDept();
            }
            else
            {
                lblMes.Text = "保存失败";
            }
        }

        protected void btnDeleteDept_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(lblDeptID.Text.Trim()))
            {
                lblMes.Text = "请选择要删除的部门";
            }
            int count = sdb.DELETE(lblDeptID.Text, lognUser.USER_NAME);
            if (count > 0)
            {
                lblMes.Text = "删除成功";
                BindDept();
            }
            else
            {
                lblMes.Text = "删除失败";
            }
        }

        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnHS_Click(object sender, EventArgs e)
        {
            if (div_Company.Visible == false)
            {
                div_Company.Visible = true;
                div_Dept.Visible = false;
                BindCom();
            }
            else
            {
                div_Company.Visible = false;
                div_Dept.Visible = true;
                BindDept();

                DataTable dt = new Sys_CompanyBLL().Query("SELECT * FROM SYS_COMPANY");// WHERE CMY_PARENTID=" + 1);
                ddlDeptCompany.DataSource = dt;
                ddlDeptCompany.DataTextField = "CMY_NAME";
                ddlDeptCompany.DataValueField = "CMY_ID";
                ddlDeptCompany.DataBind();
                ddlDeptCompany.Items.Insert(0, new ListItem("--请选择--", "-1"));
            }
        }

        protected void gvDept_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            try
            {
                string ids = ((Label)gvDept.Rows[e.NewSelectedIndex].FindControl("lblDId")).Text;

                Sys_Department model = sdb.GetInfoById(ids);

                DataTable dt = new Sys_CompanyBLL().Query("SELECT * FROM SYS_COMPANY");// WHERE CMY_PARENTID=" + 1);
                ddlDeptCompany.DataSource = dt;
                ddlDeptCompany.DataTextField = "CMY_NAME";
                ddlDeptCompany.DataValueField = "CMY_ID";
                ddlDeptCompany.DataBind();
                ddlUpDept.Items.Insert(0, new ListItem("---请选择---", "-1"));

                if (model != null)
                {
                    lblDeptID.Text = model.DEPTID.ToString();
                    txtDeptName.Text = model.DEPTNAME.ToString();
                    ddlDeptCompany.SelectedValue = model.CMY_ID.ToString();

                    DataTable ds = new Sys_DepartmentBLL().Query("SELECT * FROM Sys_Department WHERE CMY_ID=" + ddlDeptCompany.SelectedValue);// + wheres);
                    ddlUpDept.DataSource = ds;
                    ddlUpDept.DataTextField = "DEPTNAME";
                    ddlUpDept.DataValueField = "DEPTID";
                    ddlUpDept.DataBind();
                    ddlUpDept.Items.Insert(0, new ListItem("---请选择---", "-1"));
                    ddlUpDept.SelectedValue = model.DEPTPARENTID.ToString() == "0" ? "-1" : model.DEPTPARENTID.ToString();

                    DropDownList1.SelectedValue = model.DLEVEL.ToString();
                    DropDownList2.SelectedValue = model.STATE.ToString();
                    txtDeptNumber.Text = model.DEPTNUMBER.ToString();
                    if (model.DEPT_PERSONID.ToString() == "0")
                    {
                        Name1.Text = "";
                        HId1.Value = "";
                    }
                    else
                    {
                        Name1.Text = sub.GetInfoById(model.DEPT_PERSONID.ToString()).USER_NAME;
                        HId1.Value = model.DEPT_PERSONID.ToString();
                    }
                    if (model.DEPT_DEPERSONID.ToString() == "0")
                    {
                        Name2.Text = "";
                        HId2.Value = "";
                    }
                    else
                    {
                        Name2.Text = sub.GetInfoById(model.DEPT_DEPERSONID.ToString()).USER_NAME;
                        HId2.Value = model.DEPT_DEPERSONID.ToString();
                    }
                    if (model.DEPT_JXPERSONID.ToString() == "0")
                    {
                        Name3.Text = "";
                        HId3.Value = "";
                    }
                    else
                    {
                        Name3.Text = sub.GetInfoById(model.DEPT_JXPERSONID.ToString()).USER_NAME;
                        HId3.Value = model.DEPT_JXPERSONID.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Page, ex.Message);
            }
        }

        /// <summary>
        /// GridView标题列排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvCompany_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindProjectName(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        public void BindProjectName(bool iSsort)
        {
            DataTable dt = null;

            DataView view = dt.DefaultView;
            if (iSsort)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;//设置排序
            }

            gvCompany.DataSource = view;

            gvCompany.DataBind();
        }
    }
}